<template>
  <div class="summary-outline-wrap">
    <div class="summary-outline-btn">
      <div class="outline-btn-edit">
        <span v-show="!isEdit" class="mr20" @click="editHandler">编辑</span>
        <p class="mr20" v-show="isEdit">
          <span class="mr20" @click="saveHandler">保存</span>
          <span @click="cancelHandler">取消</span>
        </p>
      </div>
      <span class="export-btn" v-pdf="{ el: 'item-detail-p3', title: '项目成员分工表' }">导出</span>
      <!-- <div class="print" v-print="{ el: `#gf`, popTitle: '定制信息' }">打印</div> -->
    </div>
    <div class="item-detail-p3">
      <div class="summary-outline-title">项目成员分工表</div>
      <div id="gf">
        <div class="summary-outline-content">
          <div class="outline-info">
            <div class="outline-info-title">基本信息</div>
            <ul class="outline-info-list">
              <li>
                <span>项目编号：</span>
                <span>{{ detail.code || "--" }}</span>
              </li>
              <li>
                <span>客户名称：</span>
                <!-- <span>{{ detail.orderModel?.companyName || "--" }}</span> -->
                <span>{{ detail.companyName || "--" }}</span>
              </li>
              <li>
                <span>项目经理：</span>
                <span>{{ detail.userName || "--" }}</span>
              </li>
            </ul>
            <ul class="outline-info-list">
              <!-- <li>
              <span>方案负责人：</span>
              <span>测试</span>
            </li>
            <li>
              <span class="info-width">发货日期：</span>
              <span>2024/7/12</span>
            </li> -->
            </ul>
          </div>
          <div class="summary-outline-table">
            <TableContent :is-edit="isEdit" title="项目组成员" :table-options="itemList">
              <template #typeId="scope">
                <span v-show="!isEdit">{{ getDictItem("projectMember", scope.scope.typeId) || "--" }}</span>
                <el-select v-show="isEdit" v-model="scope.scope.typeId" placeholder="项目角色" clearable filterable>
                  <el-option v-for="item in getDict('projectMember')" :key="item.name" :label="item.name" :value="item.id" />
                </el-select>
              </template>
              <!-- <template #content="scope">
              <span v-show="!isEdit">{{ scope.scope.content || "--" }}</span>
              <el-input v-show="isEdit" type="textarea" v-model="scope.scope.content" autosize placeholder="职责"></el-input>
            </template> -->
              <template #userId="scope">
                <span v-show="!isEdit">{{ scope.scope.userName || "--" }}</span>
                <el-select v-show="isEdit" v-model="scope.scope.userId" placeholder="成员名称" clearable filterable>
                  <el-option v-for="item in viewUserList" :key="item.name" :label="item.name" :value="item.id" />
                </el-select>
              </template>
              <template #mobile="scope">
                <span>{{ scope.scope.mobile || "--" }}</span>
                <!-- v-show="!isEdit" -->
                <!-- <el-input type="textarea" v-show="isEdit" v-model="scope.scope.phone" autosize placeholder="联系电话"></el-input> -->
              </template>
              <template #email="scope">
                <span>{{ scope.scope.email || "--" }}</span>
                <!-- v-show="!isEdit" -->
                <!-- <el-input
                type="textarea"
                v-show="isEdit"
                v-model="scope.scope.replyContent"
                autosize
                placeholder="电子邮箱"
              ></el-input> -->
              </template>
              <template #actionOptions="scope">
                <span class="task-table-del" @click="deleteAgv(1, scope.scope)">删除</span>
              </template>
            </TableContent>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { reactive, ref, onMounted, inject, watch } from "vue";
import TableContent from "../components/TableContent/index.vue";
import { getListAllUser } from "@/api/modules/sys";
import { TableProps } from "@/components/Table/interface";
import { ElMessage } from "element-plus";
import { useDict } from "@/hooks/useDict";
import mittBus from "@/utils/mittBus";
import { addProjectUserList } from "@/api/modules/project"; // getProjectProblemAddList, deleteProjectProduct,
import { useHandleData } from "@/hooks/useHandleData";
// hook
const { getDictItem, getDict } = useDict(["projectMember"]);
// 变量
const props = defineProps({
  detail: {
    type: Object,
    default: () => {}
  },
  task: {
    type: Object,
    default: () => {}
  }
});
const viewUserList = ref([]);
const projectId = inject("projectId");
const isEdit = ref(false);
let tableDefault: TableProps = reactive({
  columns: [
    {
      type: "index",
      title: "序号",
      key: "",
      width: 48
    },
    {
      title: "项目角色",
      key: "typeId"
    },
    {
      title: "成员名称",
      key: "userId"
    },
    {
      title: "联系电话",
      key: "mobile"
    },
    {
      title: "电子邮箱",
      key: "email"
    },
    {
      title: "操作",
      key: "actionOptions",
      width: 80
    }
  ]
});
let itemList = ref({
  data: [],
  columns: tableDefault.columns
});
// watch
watch(
  () => props.detail,
  () => {
    itemList.value.data = JSON.parse(JSON.stringify(props.detail.userModels));
  },
  {
    immediate: true
  }
);
// 方法
const init = () => {
  itemList.value.data = JSON.parse(JSON.stringify(props.detail.userModels));
};
// 获取全部用户
const getListAllUserFn = async () => {
  const { data } = await getListAllUser();
  viewUserList.value = data;
};
const editHandler = () => {
  isEdit.value = true;
};
const cancelHandler = () => {
  init();
  isEdit.value = false;
};
const getUserName = (id: String) => {
  const item = viewUserList.value.find((item: any) => item.id === id);
  return item?.name || "";
};
const saveHandler = async () => {
  try {
    const itemLists = itemList.value.data.map(item => {
      return {
        userId: item.userId,
        typeId: item.typeId,
        projectId,
        userName: getUserName(item.userId)
      };
    });
    await addProjectUserList([...itemLists]);
    isEdit.value = false;
    mittBus.emit("ProjectFileTime");
    ElMessage.success("保存成功");
  } catch (e) {
    console.log(e);
  }
};
const deleteAgv = async (id, row) => {
  try {
    await useHandleData(
      () =>
        new Promise(resolve => {
          itemList.value.data.splice(row.serialNumber, 1);
          resolve(true);
        }),
      {},
      "删除"
    );
  } catch (e) {
    console.log(e);
  }
};

onMounted(() => {
  getListAllUserFn();
});
</script>
<style lang="scss" scoped>
.summary-outline-wrap {
  box-sizing: border-box;
  min-height: 100%;
  padding: 20px;
  background: #ffffff;
  p {
    margin: 0;
  }
  i {
    font-style: normal;
  }
  .mr20 {
    margin-right: 20px;
  }
  .mt10 {
    margin-top: 10px;
  }
  .summary-outline-btn {
    display: flex;
    justify-content: flex-end;
    font-size: 16px;
    font-weight: 400;
    color: var(--el-color-primary);
    .outline-btn-edit {
      display: flex;
      span {
        cursor: pointer;
      }
    }
  }
  .summary-outline-title {
    margin-top: 12px;
    margin-bottom: 60px;
    font-size: 26px;
    font-weight: 600;
    color: #333333;
    text-align: center;
  }
  .summary-outline-content {
    margin: 0 $project-padding;
  }
  .summary-outline-table {
    margin-top: 30px;
    overflow: hidden;
    &-content {
      ul,
      li {
        padding: 0;
        margin: 0;
        list-style: none;
      }
    }
    .table-content-list {
      display: grid;
      grid-template-columns: 1fr 1fr;
      li {
        margin-bottom: 20px;
        font-size: 14px;
        font-weight: 400;
        span:first-child {
          margin-right: 10px;
          color: #666666;
        }
        span:last-child {
          color: #333333;
        }
      }
    }
  }
  .summary-outline-other {
    margin-top: 30px;
  }
  .outline-info {
    ul,
    li {
      padding: 0;
      margin: 0;
      list-style: none;
    }
    &-title {
      margin-bottom: 20px;
      font-size: 16px;
      font-weight: 600;
      color: #333333;
    }
    &-list {
      display: grid;
      grid-template-columns: 1fr 1fr 1fr;
      column-gap: 10px;
      margin-bottom: 10px !important;
      li {
        display: flex;
        align-items: center;
        font-size: 14px;
        font-weight: 400;
        span:first-child {
          margin-right: 10px;
          color: #666666;
        }
        span:last-child {
          color: #333333;
        }
      }
      .info-width {
        width: 109px;
        text-align: right;
      }
    }
  }
  .summary-outline-custom {
    margin-top: 30px;
    :deep(.el-input),
    :deep(.el-textarea) {
      .el-input__wrapper {
        cursor: default;
        background: $project-background;
        box-shadow: none;
        .el-input__inner {
          cursor: default;
        }
      }
      .el-textarea__inner {
        cursor: default;
        resize: none;
        background: $project-background;
        box-shadow: none;
      }
    }
    :deep(.custom-title-edit) {
      .el-input__wrapper {
        cursor: default;
        background-color: #ffffff;
        box-shadow: none;
        .el-input__inner {
          cursor: default;
        }
      }
      .el-textarea__inner {
        cursor: default;
        background-color: #ffffff;
        box-shadow: none;
      }
    }
  }
  :deep(.el-select) {
    width: 100% !important;
  }
}
</style>
